from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from apps.activity.models import Gathering
from apps.activity.serializer import GatheringModelSerializer
from utils.paginate import PageNum


class ActivityAPIView(ListAPIView):
    """活动展示"""
    queryset = Gathering.objects.all()
    serializer_class = GatheringModelSerializer
    pagination_class = PageNum


class ActivityPageAPIView(APIView):
    """详细数据"""

    def get(self, request, id):
        queryset = Gathering.objects.get(id=id)

        serializer = GatheringModelSerializer(queryset)
        return Response(serializer.data)


class ActivityJoinAPIView(APIView):
    """活动报名"""

    def post(self, request, id):

        gathering = Gathering.objects.get(id=id)
        user = request.user
        # 判断当前用户是否参加当前活动
        if user not in gathering.users.all():
            gathering.users.add(user)
            return Response({
                'success': True,
                'message': '报名成功'
            })

        else:
            # 如果参加过删除记录
            gathering.users.remove(user)
            return Response({
                'success': True,
                'message': '取消报名'
            })
